import $vue from '@/main'
import io from 'socket.io-client'
import moment from 'moment'
export default {
  namespaced: true,
  state: {
    chat: null,
    order: null,
  },
  mutations: { },
  actions: {
    initChat ({state, rootState, dispatch}) {
      state.chat = io('http://47.101.133.38:303', {
        query: {
          un: rootState.user.un,
          uid: rootState.user.uid,
          type: 'pc'
        }
      })
      state.chat.on('connect', () => {})
      state.chat.on('disconnect', () => {})
      state.chat.on('system', data => {
        if (data.type === 1) { // 在线
          const find = contacts.findIndex((value) => {
            return value.Id === data.data.uid
          })
          if (find > -1) {
            state.contacts[find].status = 1
          }
        } else if (data.type === 2) { // 离开
          const find = state.contacts.findIndex((value) => {
            return value.userid === data.data.uid
          })
          if (find > -1) {
            state.contacts[find].status = 0
          }
        } else if (data.type === 3) { // 强制退出，该账号在两台不同的设备同时登录
          $vue.$router.push({
            path: '/login'
          })
          $vue.$notify.error({
            title: '提示',
            message: '您的账号已在另一台设备上登录，请重新登录，如果不是本人操作，请及时修改密码',
            duration: 5000
          })
        }
      })
      state.chat.on('chat_message', ({senderId, receiverId, message, timestamp}) => {
        const [contact, userId, contacts] = [
          rootState.chat.contact || {},
          rootState.user.uid,
          rootState.chat.contacts
        ]
        if ((senderId === userId && receiverId === contact.userid) || (receiverId === userId && senderId === contact.userid)) {
          console.log(userId)
          rootState.chat.messages.push({senderId, receiverId, message, timestamp})
        } else {
          const find = contacts.findIndex(contact => {
            return contact.userid === senderId
          })
          if (find > -1) {
            rootState.chat.tabs[1].badge += 1
            rootState.chat.contacts[find].badge += 1
            state.chat.emit('add_no_read_num', {
              userId: receiverId,
              friendId: senderId
            })
          }
        }
        // 提示音
        // if (userId !== senderId) {
        //   $vue.$qq.play()
        // }
      })
      // state.socket.on('become_friend', ({senderId, receiverId}) => {
      //   dispatch('getContacts')
      // })
      // state.socket.on('private_chat', ({senderId, receiverId, message, orderId, timestamp, type}) => {
      //   state.newPrivateChat = {senderId, receiverId, message, orderId, timestamp, type}
      //   $notify.info({
      //     title: '私聊消息',
      //     message: message,
      //     duration: 6000
      //   })
      // })
      // state.socket.on('new_order', ({senderId, receiverId, message, orderId, timestamp, type}) => {
      //   state.newOrder = {senderId, receiverId, message, orderId, timestamp, type}
      //   $notify.info({
      //     title: '接单消息',
      //     message: message,
      //     duration: 6000
      //   })
      // })
    },
    sendMessage ({state, rootState}, message) {
      state.chat.emit('chat_message', {
        senderId: rootState.user.uid,
        receiverId: rootState.chat.contact.userid,
        message: message,
        timestamp: moment().valueOf()
      })
    }
  }
}
